package com.dure.hotmemory.notification;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.support.v4.app.NotificationCompat;

import com.dure.commonlib.util.ApplicationUtils;
import com.dure.hotmemory.BuildConfig;
import com.socks.library.KLog;

public class EppNotificationControl {

    NotificationManager mNotificationManager;
    NotificationCompat.Builder mBuilder;
    String downloadPath;
    String url;
    String name;
    int imgId;
    int notificationId;

    int progress;
    int NOTIFYCATIONID = 1001;
    Context context;

    public EppNotificationControl(String url, String name, int imgId, String downloadPath, int notificationId, Context context) {
        this.downloadPath = downloadPath;
        this.context = context;
        this.url = url;
        this.name = name;
        this.imgId = imgId;
        NOTIFYCATIONID = notificationId;
        initNotifycation();
        KLog.i("lesleixz", downloadPath);
    }

    private void initNotifycation() {
        mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        mBuilder = new NotificationCompat.Builder(context);
        mBuilder.setWhen(System.currentTimeMillis()).setSmallIcon(
                imgId);
    }

    public void showProgressNotify() {
        mBuilder.setContentTitle(name).setContentText("")
                .setTicker("");
        Notification mNotification = mBuilder.build();
        // mNotification.flags = Notification.FLAG_ONGOING_EVENT;//
        mBuilder.setProgress(100, progress, false);
        mNotificationManager.notify(NOTIFYCATIONID, mNotification);
    }

    public void updateNotification(int progress) {
        Notification mNotification = mBuilder.build();
        mNotification.flags = Notification.FLAG_ONGOING_EVENT;
        mBuilder.setProgress(100, progress, false);
        mBuilder.setContentText(String.format("下载中%s%%...", progress)).setContentTitle(name);
        if (progress >= 100) {
            mBuilder.setContentText("").setContentTitle(name);
            // installApk();
            mNotificationManager.cancel(NOTIFYCATIONID);
//            mNotificationManager.cancelAll();
            ApplicationUtils.installApk(BuildConfig.APPLICATION_ID, downloadPath, context, mNotificationManager,
                    NOTIFYCATIONID);
        }else {
            mNotificationManager.notify(NOTIFYCATIONID, mNotification);
        }
    }

}
